package com.jt.tenant.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetMessageSetting;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/2/14.
 */
public class InfoSettingActivity extends JTActivity implements View.OnClickListener{
    private TextView title_name;
    private ImageView driving_report_notice,abnormal_shock_remind,
            electric_fence_remind,battery_state_remind,peccancy_remind,maintain_remind;

    private SetMessageSetting setMessageSetting;

    private String type = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_infosetting);
        initView();

        setMessageSetting = new SetMessageSetting(new AsyCallBack<SetMessageSetting.Info>(){

            @Override
            public void onSuccess(String toast, int type, SetMessageSetting.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    switch (info.type){

                        case "1":

                            if(info.flag.equals("0")){

                                driving_report_notice.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"1");

                            }else {

                                driving_report_notice.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"1");

                            }

                            break;

                        case "2":

                            if(info.flag.equals("0")){

                                abnormal_shock_remind.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"2");

                            }else {

                                abnormal_shock_remind.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"2");

                            }

                            break;

                        case "3":

                            if(info.flag.equals("0")){

                                electric_fence_remind.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"3");

                            }else {

                                electric_fence_remind.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"3");

                            }

                            break;

                        case "4":

                            if(info.flag.equals("0")){

                                battery_state_remind.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"4");

                            }else {

                                battery_state_remind.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"4");

                            }

                            break;

                        case "5":

                            if(info.flag.equals("0")){

                                peccancy_remind.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"5");

                            }else {

                                peccancy_remind.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"5");

                            }

                            break;



                        case "6":

                            if(info.flag.equals("0")){

                                maintain_remind.setImageResource(R.mipmap.switch_on);

                                JTApplication.jtPreferences.saveMessage(true,"6");

                            }else {

                                maintain_remind.setImageResource(R.mipmap.switch_off);

                                JTApplication.jtPreferences.saveMessage(false,"6");

                            }

                            break;


                    }

                    UtilToast.show(InfoSettingActivity.this,"设置成功");

                }else {


                    UtilToast.show(InfoSettingActivity.this,info.msg);

                }


            }
        });
    }

    private void initView() {
        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("消息设置");
        (driving_report_notice = (ImageView) findViewById(R.id.id_driving_report_notice)).setOnClickListener(this);
        (abnormal_shock_remind = (ImageView) findViewById(R.id.id_abnormal_shock_remind)).setOnClickListener(this);
        (electric_fence_remind = (ImageView) findViewById(R.id.id_electric_fence_remind)).setOnClickListener(this);
        (battery_state_remind = (ImageView) findViewById(R.id.id_battery_state_remind)).setOnClickListener(this);
        (peccancy_remind = (ImageView) findViewById(R.id.id_peccancy_remind)).setOnClickListener(this);
        (maintain_remind = (ImageView) findViewById(R.id.id_maintain_remind)).setOnClickListener(this);

        if(JTApplication.jtPreferences.readMessage("1")){

            driving_report_notice.setImageResource(R.mipmap.switch_on);

        }else{

            driving_report_notice.setImageResource(R.mipmap.switch_off);

        }

        if(JTApplication.jtPreferences.readMessage("2")){

            abnormal_shock_remind.setImageResource(R.mipmap.switch_on);

        }else{

            abnormal_shock_remind.setImageResource(R.mipmap.switch_off);

        }

        if(JTApplication.jtPreferences.readMessage("3")){

            electric_fence_remind.setImageResource(R.mipmap.switch_on);

        }else{

            electric_fence_remind.setImageResource(R.mipmap.switch_off);

        }

        if(JTApplication.jtPreferences.readMessage("4")){

            battery_state_remind.setImageResource(R.mipmap.switch_on);

        }else{

            battery_state_remind.setImageResource(R.mipmap.switch_off);

        }

        if(JTApplication.jtPreferences.readMessage("5")){

            peccancy_remind.setImageResource(R.mipmap.switch_on);

        }else{

            peccancy_remind.setImageResource(R.mipmap.switch_off);

        }

        if(JTApplication.jtPreferences.readMessage("6")){

            maintain_remind.setImageResource(R.mipmap.switch_on);

        }else{

            maintain_remind.setImageResource(R.mipmap.switch_off);

        }

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()){

            case R.id.id_driving_report_notice:

                if(JTApplication.jtPreferences.readMessage("1")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "1";

                setMessageSetting.execute(this);

                break;

            case R.id.id_abnormal_shock_remind:

                if(JTApplication.jtPreferences.readMessage("2")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "2";

                setMessageSetting.execute(this);

                break;

            case R.id.id_electric_fence_remind:

                if(JTApplication.jtPreferences.readMessage("3")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "3";

                setMessageSetting.execute(this);

                break;

            case R.id.id_battery_state_remind:

                if(JTApplication.jtPreferences.readMessage("4")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "4";

                setMessageSetting.execute(this);

                break;

            case R.id.id_peccancy_remind:

                if(JTApplication.jtPreferences.readMessage("5")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "5";

                setMessageSetting.execute(this);

                break;

            case R.id.id_maintain_remind:

                if(JTApplication.jtPreferences.readMessage("6")){

                    setMessageSetting.flag = "1";

                }else {

                    setMessageSetting.flag = "0";

                }

                setMessageSetting.type = "6";

                setMessageSetting.execute(this);

                break;


        }
    }
}
